<?php


namespace Leave\Controller;

use Think\Controller;

class IndexPCController extends Controller
{
    
    // const MODEL_NAME = 'Leave@:';
    public function index()
    {            
       header("Content-type: text/html; charset=utf-8");

        if(I('leave_date')&&I('days')&&I('reason')){
            $dd['uid']=$_SESSION["oc_home"]['uid'];
            $dd['leave_date']=strtotime(I('leave_date'));
            $dd['create_time']=time();
            $dd['days']=I('days');
            $dd['reason']=I('reason');
            $dd['type']=I('type');

            $d=M('core_leave')->add($dd);
            // $this->success('添加成功',U('editimpl'));
            echo "<script>";
            echo"alert('请假成功');";
             echo "window.location.href='./index.php?s=/Leave/IndexPC/index'; ";
            echo"</script>";

        }else{
            $typeList = M('core_leave_type')->where('status='.'1')->select();
            $this->assign('typeList',$typeList);
            $this->display(); 
        }

    }
    public function index1()
    {   
        // dump(I('go'));exit;
        if(I('go')){
            $map1['status'] = I('go');
        }else{
            $map1['status']=array('in','1,2');
        }         
        $leave = M('core_leave_view');
       
        $map['unit_id']=$_SESSION['oc_home']['unit'];
        $map['class_id']=$_SESSION['oc_home']['class'];
        $map['uid']=$_SESSION['oc_home']['uid'];
        $map['status']=array('in','1,2');
        $count1       = $leave->where($map)->where('status=1')->count();
        $count2       = $leave->where($map)->where('status=2')->count();
        $totalCount = M('core_leave_view')->where($map)->count();
        $page = new \Think\Page($totalCount,4);
        $limit = $page->firstRow . ',' . $page->listRows . ',' . $page->totalRows;
        $list = M('core_leave_view')->where($map)->where($map1)->limit($limit)->order('id desc')->select();
        // $list       = $leave->where($map)->select();
        // foreach ($list as $key => $value) {
        //     $leave = M('core_leave_type')->where('id = '.$value['type'])->getField('title');
        //     $list[$key]['lname'] = $leave;
        // }
         $this->assign('_page',$page->show());
         $this->assign('list',$list);
         $this->assign('go',I('go'));
         $this->assign('count',$totalCount);
         $this->assign('count1',$count1);
         $this->assign('count2',$count2);
         $this->display();
    }


    public function teacherleave()
    {
        header("Content-type: text/html; charset=utf-8");
        if(IS_POST){
            $data['status'] = I('status');
            $result = M('core_leave')->where('id = '.I('tid'))->save($data);
            if($result){
                echo "<script>";
                echo"alert('更改状态成功');";
                echo "window.location.href='./index.php?s=/Leave/IndexPC/teacherleave'; ";
                echo"</script>";
            }else{
                echo "<script>";
                echo"alert('更改状态失败');";
                echo "window.location.href='./index.php?s=/Leave/IndexPC/teacherleave'; ";
                echo"</script>";
            }
        }else{           
            $map['unit_id']=$_SESSION['oc_home']['unit'];
        if($_SESSION['oc_home']['group']=='4'){
            $map['class_id'] = $_SESSION['oc_home']['class'];
        }
        $map['status']=0;
        $list=M('core_leave_view')->where($map)->select();
        $this->assign('list',$list);
        $this->display();
        }
    }

    //老师统计
    public function teacherleavedj()
    {
        if(I('nickname')){    //查询关键字进行搜索。
            $nickname = I('nickname');
            $map['nickname'] = array('like','%'.$nickname.'%');
            $this->assign('nickname',$nickname);
        }
        //按时间查询条件
        if(I('create_time')){
            $map['create_time'] = array(between,array(strtotime(I('create_time')),strtotime(I('create_time').'23:59:58')));
        }
        //通过和不通过的查询条件
        if(I('go')){
            $map3['status'] = I('go');
        }else{
            $map3['status']=array('in','1,2');
        }
        $map['unit_id']=$_SESSION['oc_home']['unit'];
        if($_SESSION['oc_home']['group']=='4'){
            $map['class_id'] = $_SESSION['oc_home']['class'];
        }
        $map['status']=array('in','1,2');
        //$list=M('core_leave_view')->where($map)->where($map3)->select();
        
        //查询已通过的类型
        $map1['status']=1;
        $succ=M('core_leave_view')->where($map)->where($map1)->count();

        //查询未通过的类型
        $map2['status']=2;
        $eroo=M('core_leave_view')->where($map)->where($map2)->count();

        //分页查询
        $totalCount = M('core_leave_view')->where($map)->where($map3)->count();
        $page = new \Think\Page($totalCount,10);
        $limit = $page->firstRow . ',' . $page->listRows . ',' . $page->totalRows;
        $this->assign('_page',$page->show());
        
        $list = M('core_leave_view')->where($map)->where($map3)->limit($limit)->order('id desc')->select();
        $count=M('core_leave_view')->where($map)->count();      
        $this->assign('list',$list);
        $this->assign('count',$count);
        $this->assign('succ',$succ);
        $this->assign('eroo',$eroo);
        $this->assign('go',I('go'));
        //dump(I('go'));
        $this->display();
    }


    public function principalleave()
    {
        //设置UTF-8编码格式
        header("Content-type: text/html;charset=utf-8");

        //查询关键字进行搜索
        if(I('nickname')){    
            $nickname = I('nickname');
            $map['nickname'] = array('like','%'.$nickname.'%');
            $this->assign('nickname',$nickname);
        }

        //根据时间段查询
        if(I('createtime')&&I('endDate')){
            $map['leave_date'] = array(between,array(strtotime(I('createtime')),strtotime(I('endDate'))));
         }

         //根据班级ID查询
        if(I('classlist')){  
            $map['class_id'] = I('classlist');
        }

        //根据状态查询
        if(I('go')){
            $map['status'] = I('go');
        }else{
            $map['status']=array('in','1,2');
        }

        switch ($_SESSION['oc_home']['group']) {
            case '3':
                $map['unit_id']=$_SESSION['oc_home']['unit'];
                $map3['status']=array('in','1,2');
                $map1['status']=1;
                $map2['status']=2;
                $classlist=M('core_class')->where('unit='.$_SESSION['oc_home']['unit'])->select();
                $succ=M('core_leave_view')->where($map)->where($map1)->count();
                $eroo=M('core_leave_view')->where($map)->where($map2)->count();
                

        //分页查询
                $totalCount = M('core_leave_view')->where($map)->count();
                $page = new \Think\Page($totalCount,2);
                $limit = $page->firstRow . ',' . $page->listRows . ',' . $page->totalRows;
                $this->assign('_page',$page->show());
                $list = M('core_leave_view')->where($map)->limit($limit)->order('id desc')->select();
                $lit=M('core_leave_view')->where('unit_id='.$_SESSION['oc_home']['unit'])->where($map3)->select();
                $count=count($lit);
                break;
            case '6':
                $fy = M('core_unit')->where('pid='.$_SESSION['oc_home']['unit'])->getField('id',true);
                $_SESSION['oc_home']['fy'] = implode(',',$fy);
                $map['unit_id']=array('in',$_SESSION['oc_home']['fy']);
                $map3['unit']=array('in',$_SESSION['oc_home']['fy']);
                $totalCount = M('core_leave_view')->where($map)->count();
                $page = new \Think\Page($totalCount,2);
                $limit = $page->firstRow . ',' . $page->listRows . ',' . $page->totalRows;
                $this->assign('_page',$page->show());
                $list = M('core_leave_view')->where($map)->limit($limit)->order('id desc')->select();
                $classlist=M('core_class')->where($map3)->select();
                $map1['status']=1;
                $map2['status']=2;
                $map4['status']=array('in','1,2');
                $succ=M('core_leave_view')->where($map)->where($map1)->count();
                $eroo=M('core_leave_view')->where($map)->where($map2)->count();
                $count=M('core_leave_view')->where($map)->where($map4)->count();
                break;
        }
        // dump($_SESSION);exit;
        $this->assign('list',$list);
        $this->assign('classlist',$classlist);
        $this->assign('count',$count);
        $this->assign('succ',$succ);
        $this->assign('eroo',$eroo);
        $this->assign('go',I('go'));
        $this->display();

    }
}

